package com.stars.home.vmodel

import android.app.Activity
import android.app.Application
import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.hwangjr.rxbus.RxBus
import com.stars.home.activity.CertificationActivity
import com.stars.home.config.ConfigRx
import com.stars.home.config.MIntentAction
import com.stars.home.fragment.AccountBillFragment
import com.stars.home.jupsh.componnent.JpushUtils
import com.stars.home.model.AmountCount
import com.stars.home.model.User
import com.stars.home.service.SocketUtils
import com.stars.home.utils.TimeUtils
import com.stars.home.utils.net.NetEngine
import com.tencent.mmkv.MMKV
import com.trailer.base.base.BRRepository
import com.trailer.base.base.BRViewModel
import com.trailer.base.net.IResp
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch

/**
 *
 *on 2020/9/18
 */
class HomeFirstVModel(application: Application) : BRViewModel<BRRepository>(application) {


    var user=MutableLiveData<User>()

    var status="1"
    var moneySort:String="0"
    var distanceSort:String="0"

    var index=MutableLiveData<Int>()




    fun toRefuse(id:String?,position:Int?){

        toSetReceiving(id,"-1",position)

    }

    fun toReceiving(id:String?,serviceTime:String?,position:Int?){

        var isRecord= MMKV.defaultMMKV().getString("isRecord","4")
        if(isRecord.equals("0")){
            toast("您还为认证！，无法接单！")
            return
        }
        if(isRecord.equals("1")){
            toast("实名认证信息审核中暂时不能接单！请等待")
            return
        }
        if(isRecord.equals("-1")){
            toast("实名认证信息审核不通过！")
            return
        }
        if(isRecord.equals("3")){
            toast("请开启接单按钮！")
            return
        }

        if(TimeUtils.timeCompare(serviceTime, TimeUtils.getTime())==3){
            toast("已超过预约时间，无法接单！")
            return
        }
        toSetReceiving(id,"1",position)

    }

    fun toSetReceiving(id:String?,type:String?,position:Int?){

        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.receiving(id,type) }
            if(it.ok(true)){
                if(type=="1"){
                    RxBus.get().post(ConfigRx.TAKE_ORDER, "")
                }else {
                    index.value = position
                }
            }
        }

    }


    fun getUserInfo(){
        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.getUser() }
            if(it.ok()){
                user.value=it.data
                //设置别名
                JpushUtils.setPushAlias(repository().context,it.data?.id!!)
                //保存
                MMKV.defaultMMKV().putString("userId", it.data?.id)

                SocketUtils.initRoom(it.data?.id)
            }
        }
    }

    fun setStatus(){

        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.updateOnline() }
            if(it.ok(true)){
                //刷新
                RxBus.get().post(ConfigRx.REFRESH_USER, "")

            }
        }

    }

    override fun apiFunc(): suspend () -> IResp<*> {

        return {
            NetEngine.service.technicianOrder(10, repository().page,status,moneySort,distanceSort)
        }
    }
}
